package com.zl.registration.controller;

import com.zl.registration.vo.RegisterVo;
import com.zl.registration.module.result.CodeMsg;
import com.zl.registration.module.result.Result;
import com.zl.registration.service.UserRetrieveService;
import com.zl.registration.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户登录页操作接口
 *
 * @author zoulei
 * @date 2021/11/19 18:00
 */
@RestController
@RequestMapping("/userLogin")
@Slf4j
public class UserLoginController {

    @Resource
    private UserService userService;

    @Resource
    private UserRetrieveService userRetrieveService;

    /**
     * 用户注册接口
     *
     * @param registerInfo 用户信息
     * @return 响应信息
     */

    @PostMapping("/register")
    public Result<CodeMsg> register(RegisterVo registerInfo) {
        CodeMsg registerResponse = userService.register(registerInfo);
        return Result.success(registerResponse);
    }

    /**
     * 用户登录接口
     *
     * @param phone, password 登录信息
     * @return 响应信息（token）
     */
    @PostMapping("/login")
    public Result<String> login(String phone, String password) {
        String token = userService.login(phone, password);
        return Result.success(token);
    }

    /**
     * 通过用户手机号码查询对应密保问题
     *
     * @param phone 用户手机号码
     * @return 密保问题
     */
    @PostMapping("/question")
    public Result<String> retrieve(String phone) {
        String questionByPhone = userRetrieveService.findQuestionByPhone(phone);
        return Result.success(questionByPhone);
    }

    /**
     * 判断密保答案是否正确
     *
     * @param phone  手机号码
     * @param answer 密保答案
     * @return 判断结果
     */
    @PostMapping("/judge")
    public Result<Boolean> judge(String phone, String answer) {
        Boolean isTrue = userRetrieveService.checkAnswer(phone, answer);
        return Boolean.TRUE.equals(isTrue) ? Result.success(CodeMsg.ANSWER_IS_CORRECT) : Result.success(CodeMsg.ANSWER_IS_WRONG);
    }

    /**
     * 重新设置密码
     *
     * @param password 重设信息
     * @return 响应结果
     */
    @PostMapping("/reSet")
    public Result<CodeMsg> reSet(String phone, String password) {
        userRetrieveService.resetPassword(phone, password);
        return Result.success(CodeMsg.RESET_PASSWORD_SUCCESS);
    }
}
